﻿
#region "命名空间"

using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Text.RegularExpressions;

#endregion

namespace ZN.Common {
    /// <summary>
    /// JavaScript脚本操作公共类
    /// </summary>
    public class JavaScript {

        public static void IncludeJs(string sUrl) {
            IncludeJs(null, null, sUrl);
        }

        public static void IncludeJs(Page oPage, string sUrl) {
            IncludeJs(oPage, null, sUrl);
        }

        public static void IncludeJs(Page oPage, string key, string sUrl) {
            if (oPage == null)
                oPage = HttpContext.Current.Handler as Page;

            if (string.IsNullOrEmpty(key))
                key = Path.GetFileName(sUrl);

            if (!oPage.ClientScript.IsClientScriptIncludeRegistered(oPage.GetType(), key)) {
                oPage.ClientScript.RegisterClientScriptInclude(oPage.GetType(), key, sUrl);
            }
        }

        public static void IncludeJs(HtmlTextWriter output, string sUrl) {
            Page oPage = HttpContext.Current.Handler as Page;
            Type t = oPage.GetType();
            if (!oPage.ClientScript.IsClientScriptBlockRegistered(t, Path.GetFileName(sUrl))) {
                oPage.ClientScript.RegisterClientScriptBlock(t, Path.GetFileName(sUrl), "");
                string sScript = string.Format("<script type='text/javascript' src='{0}'></script>", sUrl);
                output.Write(sScript);
            }
        }

        public static void RegStartUpScript(string sScript) {
            RegStartUpScript(null, null, sScript);
        }

        public static void RegStartUpScript(Page oPage, string sScript) {
            RegStartUpScript(oPage, null, sScript);
        }

        public static void RegStartUpScript(Page oPage, string sKey, string sScript) {

            if (oPage == null)
                oPage = HttpContext.Current.Handler as Page;

            if (string.IsNullOrEmpty(sKey))
                sKey = Guid.NewGuid().ToString();

            bool isAddScriptTag = false;

            if (!Regex.IsMatch(sScript.Trim(), @"^\<\s*script.*[^/]\>", RegexOptions.IgnoreCase))
                isAddScriptTag = true;

            if (!oPage.ClientScript.IsStartupScriptRegistered(oPage.GetType(), sKey)) {
                oPage.ClientScript.RegisterStartupScript(oPage.GetType(), sKey, sScript, isAddScriptTag);
            }
        }

        public static void RegScriptBlock(string sScript) {
            RegScriptBlock(null, null, sScript);
        }

        public static void RegScriptBlock(Page oPage, string sScript) {
            RegScriptBlock(oPage, null, sScript);
        }

        public static void RegScriptBlock(Page oPage, string key, string sScript) {

            if (oPage == null)
                oPage = HttpContext.Current.Handler as Page;

            if (string.IsNullOrEmpty(key))
                key = Guid.NewGuid().ToString();

            bool isAddScriptTag = false;

            if (!Regex.IsMatch(sScript.Trim(), @"^\<\s*script.*[^/]\>", RegexOptions.IgnoreCase))
                isAddScriptTag = true;

            if (!oPage.ClientScript.IsClientScriptBlockRegistered(oPage.GetType(), key)) {
                oPage.ClientScript.RegisterClientScriptBlock(oPage.GetType(), key, sScript, isAddScriptTag);
            }
        }

        public static void IncludeCss(string sUrl) {
            IncludeCss(null, null, sUrl);
        }

        public static void IncludeCss(Page oPage, string sUrl) {
            IncludeCss(oPage, null, sUrl);
        }

        public static void IncludeCss(Page oPage, string sKey, string sUrl) {

            if (oPage == null)
                oPage = HttpContext.Current.Handler as Page;

            if (string.IsNullOrEmpty(sKey)) {
                sKey = Path.GetFileName(sUrl);
            }
            string sResourceUrl = string.Format("<link  type='text/css' rel='stylesheet' href='{0}'></link>", sUrl);

            if (oPage.Header != null && !oPage.ClientScript.IsClientScriptBlockRegistered(oPage.GetType(), sKey)) {
                oPage.ClientScript.IsClientScriptBlockRegistered(oPage.GetType(), sKey);
                oPage.Header.Controls.Add(new LiteralControl(sResourceUrl));
            }
        }

        public static void Alert(object oMessage) {
            Alert(null, oMessage);
        }

        public static void Alert(Page oPage, object oMessage) {
            RegScriptBlock(oPage, string.Format("alert({0});", Utils.ObjectToJavaScriptString(oMessage)));
        }

        public static void AlertAndRedirect(Page oPage, object oMessage, string sUrl) {
            if (oPage != null) sUrl = oPage.ResolveClientUrl(sUrl);
            RegScriptBlock(oPage, string.Format("alert({0});location.href={1}", Utils.ObjectToJavaScriptString(oMessage), Utils.ObjectToJavaScriptString(sUrl)));
        }
    }
}
